#include "mainpage.h"

TaskMgr task_mgr; //任务管理器
LP load_page;     //开始页面
CG choose_game;   //选择游戏页面
box_CL box_choose_level; //选择扫雷关卡界面
lv_obj_t *SettingPage;//设置界面
lv_obj_t *ExtraPage;//额外界面
extern lv_indev_t *indev_keypad;

static void taskMgr_callback(lv_task_t *t)
{
    //在登陆窗口运行时判断
    if (task_mgr.isLoadingPageRunning == 1) {
        if (task_mgr.Destory_LP_request == 1) {
            del_LP(&load_page);
            create_CG(&choose_game);
            if(infoBox.enableBGM)
                OrderSong(2);
            task_mgr.isLoadingPageRunning = 0;//更新本窗口状态
            task_mgr.isChooseGamePageRunning = 1;//更新被打开窗口的状态
            task_mgr.Destory_LP_request=0;//已完成处理请求标记
        }
    }
    //选择游戏窗口运行时判断
    if (task_mgr.isChooseGamePageRunning == 1) {
        //关闭选择游戏界面的同时，选择了推箱子游戏
        if (task_mgr.Enter_BC_request == 1) { 
            del_CG(&choose_game);
            create_box_level(&box_choose_level);
            task_mgr.isChooseGamePageRunning = 0;
            task_mgr.isBoxChoosePageRunning = 1;
            task_mgr.Enter_BC_request=0;
        }
        //关闭选择游戏界面的同时，选择了Extra
        if (task_mgr.Enter_Extra_request == 1) { 
            del_CG(&choose_game);
            ExtraPage = createExtra();
            task_mgr.isChooseGamePageRunning = 0;
            task_mgr.isExtraPageRunning = 1;
            task_mgr.Enter_Extra_request=0;
        }
        //关闭选择游戏界面的同时，选择了系统设置
        if (task_mgr.Enter_setting_request == 1) {
            del_CG(&choose_game);
            SettingPage = createSettingPage();
            task_mgr.isChooseGamePageRunning = 0;
            task_mgr.isSettingPageRunning = 1;
            task_mgr.Enter_setting_request=0;
        }
    }
    //推箱子选择关卡界面
    if (task_mgr.isBoxChoosePageRunning == 1) {
        if (task_mgr.BackTo_CG_request == 1) { //关闭关卡选择界面的同时，选择了回到上一个界面
            del_box_level(&box_choose_level);
            create_CG(&choose_game);
            task_mgr.isBoxChoosePageRunning = 0;
            task_mgr.isChooseGamePageRunning = 1;
            task_mgr.BackTo_CG_request=0;
        }
        if (task_mgr.Enter_BoxGame_request == 1) { //关闭关卡选择界面的同时，选择了回到上一个界面
            del_box_level(&box_choose_level);
            create_box_game();
            if(infoBox.enableBGM)
                OrderSong(3);
            task_mgr.isBoxChoosePageRunning = 0;
            task_mgr.isGamePageRunning = 1;
            task_mgr.Enter_BoxGame_request=0;
        }
    }
    //推箱子游戏界面
    if (task_mgr.isGamePageRunning == 1) {
        if (task_mgr.Exit_Game_request == 1) {
            delete_GamePage();
            create_box_level(&box_choose_level);
            if(infoBox.enableBGM)
                OrderSong(2);
            task_mgr.isGamePageRunning = 0;
            task_mgr.isBoxChoosePageRunning = 1;
            task_mgr.Exit_Game_request=0;
        }
        if (task_mgr.Refresh_Game_request == 1) {
            refresh_GamePage();
            task_mgr.Refresh_Game_request=0;
        }
    }
    //在设置界面进行判断
    if (task_mgr.isSettingPageRunning == 1) {
        if (task_mgr.BackTo_CG_request == 1) {
            load_settingData_toSD(&infoBox);
            lv_obj_del(SettingPage);
            create_CG(&choose_game);
            task_mgr.isSettingPageRunning = 0;//更新本窗口状态
            task_mgr.isChooseGamePageRunning = 1;//更新被打开窗口的状态
            task_mgr.BackTo_CG_request=0;//已完成处理请求标记
        }
    }
    //在额外页面进行判断
    if (task_mgr.isExtraPageRunning == 1) {
        if (task_mgr.BackTo_CG_request == 1) {
            lv_obj_del(ExtraPage);
            create_CG(&choose_game);
            task_mgr.isExtraPageRunning = 0;//更新本窗口状态
            task_mgr.isChooseGamePageRunning = 1;//更新被打开窗口的状态
            task_mgr.BackTo_CG_request=0;//已完成处理请求标记
        }
    }
    //之后的窗口...后面再写
}

static void obj_bar_anim_exec_callback(void *bar, int32_t value)
{
    if (bar != NULL) {
        lv_bar_set_value((lv_obj_t *)bar, value, LV_ANIM_ON); // 设置进度条对象的值
    }
    if (value == 100)
        task_mgr.Destory_LP_request = 1;
}

void starting_up()
{
    
    //创建任务管理器
    taskmgrInit(&task_mgr);
    lv_task_create(taskMgr_callback, 100, LV_TASK_PRIO_MID, NULL); //创建任务 100ms一次
    
    //当前屏幕对象
    src = lv_scr_act();
    lv_style_init(&load_page.img_style);                                        //初始化sytle
    lv_style_set_image_opa(&load_page.img_style, LV_STATE_DEFAULT, LV_OPA_100); //设置背景透明度
    
    load_page.imgMihoyo = lv_img_create(src, NULL);
    lv_img_set_src(load_page.imgMihoyo, "F:/mihoyo.bin");
    lv_obj_add_style(load_page.imgMihoyo, LV_OBJ_PART_MAIN, &load_page.img_style); //设置样式
    
    load_page.main_container = lv_obj_create(lv_scr_act(), NULL);
    lv_obj_set_size(load_page.main_container, 300, 100);           // 设置尺寸
    lv_obj_set_pos(load_page.main_container, 250, 340);
    
    load_page.obj_label = lv_label_create(load_page.main_container, NULL);
    if (load_page.obj_label != NULL) {
        // lv_obj_get_style_text_color(obj_label,LV_COLOR_WHITE); // 设置文本颜色
        lv_label_set_text(load_page.obj_label, "Tech Otaku Save the World"); // 设置文本内容
        lv_obj_align(load_page.obj_label, NULL, LV_ALIGN_CENTER, 0, -30);
    }
    
    //滑动条的style
    lv_style_init(&load_page.style_bg);
    lv_style_reset(&load_page.style_bg);
    lv_style_set_border_color(&load_page.style_bg, LV_STATE_DEFAULT, lv_color_hex(0xFFFFFF)); // 设置边框颜色
    lv_style_set_border_width(&load_page.style_bg, LV_STATE_DEFAULT, 2);                      // 设置边框宽度
    lv_style_set_pad_all(&load_page.style_bg, LV_STATE_DEFAULT, 6);
    lv_style_set_radius(&load_page.style_bg, LV_STATE_DEFAULT, 6); // 设置圆角弧度

    lv_style_reset(&load_page.style_indic);
    lv_style_init(&load_page.style_indic);
    lv_style_set_bg_color(&load_page.style_indic, LV_STATE_DEFAULT, lv_color_hex(0xFFFFFF));
    lv_style_set_radius(&load_page.style_indic, LV_STATE_DEFAULT, 3);
    
    load_page.obj_bar = lv_bar_create(load_page.main_container, NULL);
    if (load_page.obj_bar != NULL) {
        // lv_obj_remove_style(obj_bar);  // 清除所有样式
        lv_obj_add_style(load_page.obj_bar, LV_OBJ_PART_MAIN, &load_page.style_bg);
        lv_obj_add_style(load_page.obj_bar, LV_OBJ_PART_ALL, &load_page.style_indic);
        lv_obj_set_size(load_page.obj_bar, 200, 20);
        lv_obj_align(load_page.obj_bar, LV_STATE_DEFAULT, LV_ALIGN_CENTER, 0, 30);

        lv_anim_init(&load_page.anim);                                    // 初始化动画
        lv_anim_set_exec_cb(&load_page.anim, obj_bar_anim_exec_callback); // 添加回调函数
        lv_anim_set_time(&load_page.anim, 5000);                          // 设置动画时长
        lv_anim_set_var(&load_page.anim, load_page.obj_bar);              // 动画绑定对象
        lv_anim_set_values(&load_page.anim, 0, 100);                      // 设置开始值和结束值
        lv_anim_set_repeat_count(&load_page.anim, 1);                     // 重复次数，默认值为1 LV_ANIM_REPEAT_INFINIT用于无限重复
        lv_anim_start(&load_page.anim);                                   // 应用动画效果
    }
    printf_Mem();
}
